(function() {

this.pasta = this.pasta || {};
this.pasta.webgui = this.pasta.webgui || {};

this.pasta.webgui.DatePickerPanel = pasta.webgui.ContainerControl.extend(
{
	init : function()
	{
		this._super();
		
		this.canvas.style.zIndex = "9999999";
		
		this.setBgColor("#FFFFFF");
		this.setBorderColor("#000000");
		
		this.setWidth(180);
		this.setHeight(170);
		
		this.canvas.style.webkitBoxShadow = "0px 3px 6px #000000";
		this.canvas.style.mozBoxShadow = "0px 3px 6px #000000";
		this.canvas.style.boxShadow = "0px 3px 6px #000000";
		
		this.pnlHeader = new pasta.webgui.Shape();
		this.pnlHeader.setLeft(0);
		this.pnlHeader.setTop(0);
		this.pnlHeader.setHeight(28);
		this.pnlHeader.setWidth(this.getWidth());
		this.pnlHeader.setBgColor("#228B22");
		this.pnlHeader.setBorderColor();
		this.addControl(this.pnlHeader);
		
		this.btnYearPrev = new pasta.webgui.ImageButton();
		this.btnYearPrev.setLeft(5);
		this.btnYearPrev.setTop(2);
		this.btnYearPrev.setWidth(24);
		this.btnYearPrev.setHeight(24);
		this.btnYearPrev.setImage(this.libDir + "/img/control/datePicker/yearPrev.png");
		this.btnYearPrev.getOnClick().setHandler(this, this.btnYearPrevClick);
		this.addControl(this.btnYearPrev);
		
		this.btnPrev = new pasta.webgui.ImageButton();
		this.btnPrev.setLeft(this.btnYearPrev.getLeft() + this.btnYearPrev.getWidth());
		this.btnPrev.setTop(this.btnYearPrev.getTop());
		this.btnPrev.setWidth(this.btnYearPrev.getWidth());
		this.btnPrev.setHeight(this.btnYearPrev.getHeight());
		this.btnPrev.setImage(this.libDir + "/img/control/datePicker/prev.png");
		this.btnPrev.getOnClick().setHandler(this, this.btnPrevClick);
		this.addControl(this.btnPrev);
		
		this.btnYearNext = new pasta.webgui.ImageButton();
		this.btnYearNext.setWidth(this.btnYearPrev.getWidth());
		this.btnYearNext.setHeight(this.btnYearPrev.getHeight());
		this.btnYearNext.setLeft(this.getWidth() - this.btnYearNext.getWidth() - 5);
		this.btnYearNext.setTop(this.btnYearPrev.getTop());
		this.btnYearNext.setImage(this.libDir + "/img/control/datePicker/yearNext.png");
		this.btnYearNext.getOnClick().setHandler(this, this.btnYearNextClick);
		this.addControl(this.btnYearNext);
		
		this.btnNext = new pasta.webgui.ImageButton();
		this.btnNext.setLeft(this.btnYearNext.getLeft() - this.btnYearNext.getWidth());
		this.btnNext.setTop(this.btnYearPrev.getTop());
		this.btnNext.setWidth(this.btnYearPrev.getWidth());
		this.btnNext.setHeight(this.btnYearPrev.getHeight());
		this.btnNext.setImage(this.libDir + "/img/control/datePicker/next.png");
		this.btnNext.getOnClick().setHandler(this, this.btnNextClick);
		this.addControl(this.btnNext);
		
		this.lblTitle = new pasta.webgui.Label();
		this.lblTitle.setLeft(this.btnPrev.getLeft() + this.btnPrev.getWidth());
		this.lblTitle.setTop(this.btnPrev.getTop() + 3);
		this.lblTitle.setWidth(this.btnNext.getLeft() - this.lblTitle.getLeft());
		this.lblTitle.setAlign("center");
		this.lblTitle.font.setBold(true);
		this.lblTitle.font.setColor("#FFFFFF");
		this.lblTitle.font.setSize(14);
		this.lblTitle.setCaption("Mar 2011");
		this.addControl(this.lblTitle);
		
		this.pnlHeaderDate = new pasta.webgui.Shape();
		this.pnlHeaderDate.setLeft(0);
		this.pnlHeaderDate.setTop(this.pnlHeader.getTop() + this.pnlHeader.getHeight());
		this.pnlHeaderDate.setHeight(18);
		this.pnlHeaderDate.setWidth(this.getWidth());
		this.pnlHeaderDate.setBgColor("#404040");
		this.pnlHeaderDate.setBorderColor();
		this.addControl(this.pnlHeaderDate);
		
		var itemWidth = 25;
		
		this.lblSenin = new pasta.webgui.Label();
		this.lblSenin.setLeft(1);
		this.lblSenin.setTop(this.pnlHeaderDate.getTop() + 1);
		this.lblSenin.setWidth(itemWidth);
		this.lblSenin.setHeight(20);
		this.lblSenin.setAlign("center");
		this.lblSenin.setCaption("S");
		this.lblSenin.font.setBold(true);
		this.lblSenin.font.setColor("#FFFFFF");
		this.addControl(this.lblSenin);
		
		this.lblSelasa = new pasta.webgui.Label();
		this.lblSelasa.setLeft(this.lblSenin.getLeft() + this.lblSenin.getWidth());
		this.lblSelasa.setTop(this.lblSenin.getTop());
		this.lblSelasa.setWidth(this.lblSenin.getWidth());
		this.lblSelasa.setHeight(this.lblSenin.getHeight());
		this.lblSelasa.setAlign("center");
		this.lblSelasa.setCaption("S");
		this.lblSelasa.font.setBold(true);
		this.lblSelasa.font.setColor("#FFFFFF");
		this.addControl(this.lblSelasa);
		
		this.lblRabu = new pasta.webgui.Label();
		this.lblRabu.setLeft(this.lblSelasa.getLeft() + this.lblSelasa.getWidth());
		this.lblRabu.setTop(this.lblSenin.getTop());
		this.lblRabu.setWidth(this.lblSenin.getWidth());
		this.lblRabu.setHeight(this.lblSenin.getHeight());
		this.lblRabu.setAlign("center");
		this.lblRabu.setCaption("R");
		this.lblRabu.font.setBold(true);
		this.lblRabu.font.setColor("#FFFFFF");
		this.addControl(this.lblRabu);
		
		this.lblKamis = new pasta.webgui.Label();
		this.lblKamis.setLeft(this.lblRabu.getLeft() + this.lblRabu.getWidth());
		this.lblKamis.setTop(this.lblSenin.getTop());
		this.lblKamis.setWidth(this.lblSenin.getWidth());
		this.lblKamis.setHeight(this.lblSenin.getHeight());
		this.lblKamis.setAlign("center");
		this.lblKamis.setCaption("K");
		this.lblKamis.font.setBold(true);
		this.lblKamis.font.setColor("#FFFFFF");
		this.addControl(this.lblKamis);
		
		this.lblJumat = new pasta.webgui.Label();
		this.lblJumat.setLeft(this.lblKamis.getLeft() + this.lblKamis.getWidth());
		this.lblJumat.setTop(this.lblSenin.getTop());
		this.lblJumat.setWidth(this.lblSenin.getWidth());
		this.lblJumat.setHeight(this.lblSenin.getHeight());
		this.lblJumat.setAlign("center");
		this.lblJumat.setCaption("J");
		this.lblJumat.font.setBold(true);
		this.lblJumat.font.setColor("#FFFFFF");
		this.addControl(this.lblJumat);
		
		this.lblSabtu = new pasta.webgui.Label();
		this.lblSabtu.setLeft(this.lblJumat.getLeft() + this.lblJumat.getWidth());
		this.lblSabtu.setTop(this.lblSenin.getTop());
		this.lblSabtu.setWidth(this.lblSenin.getWidth());
		this.lblSabtu.setHeight(this.lblSenin.getHeight());
		this.lblSabtu.setAlign("center");
		this.lblSabtu.setCaption("S");
		this.lblSabtu.font.setBold(true);
		this.lblSabtu.font.setColor("#FFD800");
		this.addControl(this.lblSabtu);
		
		this.lblMinggu = new pasta.webgui.Label();
		this.lblMinggu.setLeft(this.lblSabtu.getLeft() + this.lblSabtu.getWidth());
		this.lblMinggu.setTop(this.lblSenin.getTop());
		this.lblMinggu.setWidth(this.lblSenin.getWidth());
		this.lblMinggu.setHeight(this.lblSenin.getHeight());
		this.lblMinggu.setAlign("center");
		this.lblMinggu.setCaption("M");
		this.lblMinggu.font.setBold(true);
		this.lblMinggu.font.setColor("#FF0000");
		this.addControl(this.lblMinggu);
		
		this.setVisible(false);
		
		var left = 1;
		var top = 47;
		this.lblList = new pasta.util.ArrayList();
		
		for (var i = 1; i <= 36; i++)
		{
			var lbl = new pasta.webgui.Label();
			lbl.setLeft(left);
			lbl.setTop(top);
			lbl.setWidth(itemWidth);
			lbl.setHeight(20);
			lbl.setAlign("center");
			lbl.setCaption("" + i);
			lbl.setButtonMode(true);
			lbl.getOnClick().setHandler(this, this.lblClick);
			this.addControl(lbl);
			
			this.lblList.add(lbl);
			
			if ((i % 7) == 0)
			{
				lbl.font.setColor("#FF0000");
				
				left = 1;
				top += 20;
			}
			else
			{
				if ((i % 7) == 6)
					lbl.font.setColor("#FFD800");
				
				left += itemWidth;
			}
		}
		
		var obj = this;
		this.canvas.onmousedown = function(e) { if (!e) e = window.event; obj.doMouseDown(e.layerX, e.layerY); };
		this.focused = false;
		
		this.year = 2011;
		this.month = 1;
		this.date = 1;
		
		this.selectedYear = 2011;
		this.selectedMonth = 1;
		this.selectedDate = 1;
	},
	
	lblClick : function(sender)
	{
		if (this._onSelect != null)
		{
			var dt = parseInt(sender.getCaption());
			
			this._onSelect.callHandler(this, this.year + "-" + (this.month < 10 ? ("0" + this.month) : ("" + this.month)) + "-" +
											(dt < 10 ? ("0" + dt) : ("" + dt)));
		}
	},
	
	btnYearPrevClick : function(sender)
	{
		this.year--;
		
		this.reorganize();
	},
	
	btnYearNextClick : function(sender)
	{
		this.year++;
		
		this.reorganize();
	},
	
	btnPrevClick : function(sender)
	{
		this.month--;
		
		if (this.month == 0)
		{
			this.month = 12;
			this.year--;
		}
		
		this.reorganize();
	},
	
	btnNextClick : function(sender)
	{
		this.month++;
		
		if (this.month == 13)
		{
			this.month = 1;
			this.year++;
		}
		
		this.reorganize();
	},
	
	setDate : function(dateString)
	{
		dateString = "" + dateString;
		
		var yy;
		var mm;
		var dd;
		
		if (dateString.match(/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/))
		{
			//2011-01-01
			//0123456789
			this.year = parseInt(dateString.substr(0, 4));
			this.month = parseInt(dateString.substr(5, 2));
			this.date = parseInt(dateString.substr(8, 2));
		}
		else
		{
			var dt = new Date();
			
			this.year = dt.getFullYear();
			this.month = dt.getMonth() + 1;
			this.date = dt.getDate();
		}
		
		this.selectedYear = this.year;
		this.selectedMonth = this.month;
		this.selectedDate = this.date;
		
		this.reorganize();
	},
	
	reorganize : function()
	{
		var dt = new Date(this.year, this.month - 1, 1);
		var currDate = new Date();
		
		var start = dt.getDay() - 1;
		
		if (dt.day == 0)
			start = 6;
		
		i = 0;
		
		while (i < start)
		{
			this.lblList.get(i).setVisible(false);
			i++;
		}
		
		var dtNow = 1;
		var maxDate = this.getMaxDate();
		
		while (dtNow <= maxDate)
		{
			var lbl = this.lblList.get(i);
			
			lbl.setVisible(true);
			lbl.setCaption("" + dtNow);
			
			lbl.setBorderColor();
			lbl.setBgColor("");
			
			if ((dtNow == this.selectedDate) && (this.selectedMonth == this.month) && (this.selectedYear == this.year))
			{
				lbl.setBgColor("#C0C0C0");
				lbl.setBorderColor();
			}
			
			if ((dtNow == currDate.getDate()) && (this.month == (currDate.getMonth() + 1)) && (this.year == currDate.getFullYear()))
				lbl.setBorderColor("#FF0000");
			
			dtNow++;
			i++;
		}
		
		while (i < 36)
		{
			this.lblList.get(i).setVisible(false);
			i++;
		}
		
		this.lblTitle.setCaption(this.getMonthName() + " " + this.year);
	},
	
	getMonthName : function()
	{
		var result = "Jan";
		
		switch (this.month)
		{
			case 2 :
					result = "Feb";
					break;
			case 3 :
					result = "Mar";
					break;
			case 4 :
					result = "Apr";
					break;
			case 5 :
					result = "Mei";
					break;
			case 6 :
					result = "Jun";
					break;
			case 7 :
					result = "Jul";
					break;
			case 8 :
					result = "Agu";
					break;
			case 9 :
					result = "Sep";
					break;
			case 10 :
					result = "Okt";
					break;
			case 11 :
					result = "Nov";
					break;
			case 12 :
					result = "Des";
					break;
		}
		
		return result;
	},
	
	getMaxDate : function()
	{
		var result = 31;
		
		switch (this.month)
		{
			case 2 :
					if ((this.year % 4) == 0)
						result = 29;
					else
						result = 28;
					break;
			case 4 :
			case 6 :
			case 9 :
			case 11 :
					result = 30;
					break;
			
		}
		
		return result;
	},
	
	doMouseDown : function(x, y)
	{
		this._super(x, y);
		
		this.focused = true;
	},
	
	isFocused : function()
	{
		return this.focused;
	},
	
	clearFocus : function()
	{
		this.focused = false;
	},
	
	show : function()
	{
		this.setVisible(true);
	},
	
	close : function()
	{
		this.setVisible(false);
	},
	
	getOnSelect : function()
	{
		if (this._onSelect == null)
			this._onSelect = new pasta.util.EventObj();
		
		return this._onSelect;
	},	
});

this.pasta.webgui.DatePicker = pasta.webgui.Control.extend(
{
	init : function() 
	{
		this._super();
		
		this.caption = "";
		
		var id = this.cid;
		
		this.setHeight(18);
		this.password = false;
		
		this.obj = document.createElement("input");
		this.obj.type = "text";
		this.obj.id = this.cid + "_obj";
		this.obj.style.position = "absolute";
		this.obj.style.left = "0px";
		this.obj.style.top = "0px";
		this.obj.style.width = "100%";
		this.obj.style.height = "100%";
		this.canvas.appendChild(this.obj);
		
		var obj = this;
		
		this.obj.onfocus = function(e) { obj.doFocus(); };
		this.obj.onblur = function(e) { obj.doLostFocus(); };
		this.obj.onmousedown = function(e) { obj.doFocus(); };
		
		if (pasta.webgui.DatePicker.panel == null)
		{
			pasta.webgui.DatePicker.panel = new pasta.webgui.DatePickerPanel();
			stage.addControl(pasta.webgui.DatePicker.panel);
		}
	},
	
	//------------------ Event Handler -----------------
	
	doFocus : function()
	{
		var rect = this.getClientRect();
		
		pasta.webgui.DatePicker.panel.setLeft(rect.left);
		pasta.webgui.DatePicker.panel.setTop(rect.top + this.getHeight() + 6);
		
		if (!pasta.webgui.DatePicker.panel.isVisible())
		{
			pasta.webgui.DatePicker.panel.setDate(this.getDate());
			pasta.webgui.DatePicker.panel.show();
			pasta.webgui.DatePicker.panel.getOnSelect().setHandler(this, this.panelSelect);
		}
	},
	
	panelSelect : function(sender, dateString)
	{
		this.setDate(dateString);
		pasta.webgui.DatePicker.panel.clearFocus();
		pasta.webgui.DatePicker.panel.close();
	},
	
	doLostFocus : function()
	{
		var target = this;
		setTimeout(function() {target.doCheckLostFocus();}, 100);
	},
	
	doCheckLostFocus : function()
	{
		if (pasta.webgui.DatePicker.panel.isFocused())
		{
			this.obj.focus();
			pasta.webgui.DatePicker.panel.clearFocus();
		}
		else
			pasta.webgui.DatePicker.panel.close();
	},
	
	//------------------ Setter & Getter -----------------
	
	isEnabled : function(data)
	{
		return (this.obj.disabled == false);
	},
	
	setEnabled : function(data)
	{
		if (data)
			this.obj.disabled = false;
		else
			this.obj.disabled = true;
	},
	
	isReadOnly : function(data)
	{
		return this.obj.readOnly;
	},
	
	setReadOnly : function(data)
	{
		this.obj.readOnly = data;
	},
	
	setFocus : function()
	{
		this.obj.focus();
	},
	
	getDate : function()
	{
		return this.obj.value;
	},
	
	setDate : function(data)
	{
		this.obj.value = data;
	},
	
	isPassword : function()
	{
		return this.password;
	},
	
	setPassword : function(data)
	{
		this.password = data;
		
		if (this.password)
			this.obj.type = "password";
		else
			this.obj.type = "text";
	},
	
	//--------------------------- Event -------------------------
	
	getOnKeyDown : function()
	{
		if (this._onKeyDown == null)
			this._onKeyDown = new pasta.util.EventObj();
		
		return this._onKeyDown;
	},
});

})();